﻿using System;
using System.Web.Mvc;
using ProjetoComperio.BusinessLogicLayer;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.Framework.Web.Logging;
using ProjetoComperio.Framework.Web.Security;
using ProjetoComperio.Framework.Web.Security.UrlEncrypt;
using ProjetoComperio.SystemFramework.Web;

namespace ProjetoComperio.Presentation.Web.Controllers
{
    [RequiresAuthentication]
    [ExceptionLogging]
    public class EventController : ComperioController
    {
        private EventBLL _eventBLL;
        private SubjectBLL _subjectBLL;
        private ContributionItemBLL _contributionItemBLL;

        #region Constructor
        public EventController()
        {
            _eventBLL = new EventBLL();
            _subjectBLL = new SubjectBLL();
            _contributionItemBLL = new ContributionItemBLL();
        }
        public EventController(EventBLL eventBLL)
        {
            this._eventBLL = eventBLL;
        }
        #endregion


        #region Index
        [HttpGet]
        public ActionResult Index()
        {
            ViewData.Model = _eventBLL.RetrieveEvents();
            return View();
        }
        #endregion

        #region MyIndex

        [HttpGet]
        public ActionResult MyIndex()
        {
            ViewData.Model = _eventBLL.RetrieveEvents(Convert.ToInt32(this.CurrentUser.PersonID));
            return View();
        }

        #endregion

        #region Create
        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create([Bind(Exclude = "ContributionID")] Event eventEntity)
        {
            if (!ModelState.IsValid)
                return View();

            eventEntity.AcademicPerson = new AcademicPersonBLL().RetrieveAcademicPersonByID(Convert.ToInt32(this.CurrentUser.PersonID));
            _eventBLL.Create(eventEntity);

            return RedirectToAction("Index");
        }
        #endregion

        #region Edit
        [HttpGet]
        public ActionResult Edit(String id)
        {
            Session["id"] = id;
            var eventEntity = _eventBLL.RetrieveEvent(Convert.ToInt32(URLEncrypt.DecryptFromString(id)));

            TempData["Subjects"] = _subjectBLL.RetrieveSubjects(eventEntity.ContributionID);
            TempData["ContributionItems"] = _contributionItemBLL.RetrieveContributionItems(eventEntity.ContributionID);


            return View(eventEntity);
        }

        [HttpPost]
        public ActionResult Edit([Bind(Exclude = "ContributionID")] Event eventEntity)
        {
            if (!ModelState.IsValid)
                return View(eventEntity);

            var originalEvent = _eventBLL.RetrieveEvent(Convert.ToInt32(URLEncrypt.DecryptFromString(Session["id"].ToString())));

            originalEvent.Title = eventEntity.Title;
            originalEvent.Description = eventEntity.Description;
            originalEvent.RegisterDate = eventEntity.RegisterDate;
            originalEvent.SourceInfo = eventEntity.SourceInfo;

            originalEvent.Subject = eventEntity.Subject;
            originalEvent.Location = eventEntity.Location;
            originalEvent.StartDate = eventEntity.StartDate;
            originalEvent.EndDate = eventEntity.EndDate;

            originalEvent.Details = eventEntity.Details;
            originalEvent.Author = eventEntity.Author;
            originalEvent.AuthorDescription = eventEntity.AuthorDescription;

            _eventBLL.Edit(eventEntity);
            return RedirectToAction("Index");
        }
        #endregion

        #region Visualize
        [HttpGet]
        public ActionResult Visualize(string id)
        {
            var entity = _eventBLL.RetrieveEvent(Convert.ToInt32(URLEncrypt.DecryptFromString(id)));
            return View(entity);
        }
        #endregion

    }
}
